package com.cat.dataStructure;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/resulting-string-after-adjacent-removals/description/?envType=problem-list-v2&envId=stack
 * @create 2025/9/5 23:56
 * @since JDK17
 */

public class Solution57 {
    public String resultingString(String s) {
        int n = s.length(), top = 0;
        char[] stack = new char[n];
        for (int i = 0; i < n; i++) {
            if (top > 0) {
                int a = stack[top - 1] - 'a' + 1, b = s.charAt(i) - 'a' + 1;
                if (Math.abs(a % 25 - b % 25) <= 1) {
                    top--;
                }
            } else {    //
                stack[top++] = s.charAt(i);
            }
        }
        return String.valueOf(stack, 0, top);
    }
}
